﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WPControla.Client.ViewModels.Base;
using WPControla.Client.Services;
using WPControla.Client.Services.Rss;
using WPControla.Client.Services.Podcast;

namespace WPControla.Client.ViewModels
{
    /// <summary>
    /// Application MainPage view viewmodel
    /// </summary>
    public class VMMainPage : VMBase,  IVMMainPage
    {
        private INavigationService NavService;
        private IRssService RssService;
        private IPodcastService PodcastService;
        private IAudioService AudioService;

        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="navService">navigation service</param>
        /// <param name="rssService">rss service</param>
        /// <param name="podcastService">podcast service</param>
        /// <param name="audioService"> audio playing service</param>
        public VMMainPage(INavigationService navService, IRssService rssService, IPodcastService podcastService, IAudioService audioService)
        {
            NavService = navService;
            RssService = rssService;
            PodcastService = podcastService;
            AudioService = audioService;

            RssService.RssDownloadCompleted += new EventHandler<RssDownloadEventArgs>(RssArticles_Completed);
            RssService.GetRssData(new Uri(@"http://feeds.feedburner.com/wpcontrola", UriKind.Absolute));

            PodcastService.PodcastDownloadCompleted += new EventHandler<PodcastDownloadEventArgs>(Podcast_Completed);
            podcastService.GetPodcastData(new Uri(@"http://www.wpcontrola.com/index.php/category/podcast/feed/",UriKind.Absolute));

        }

        /// <summary>
        /// Executed when the petition of podcast has been completed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Podcast_Completed(object sender, PodcastDownloadEventArgs e)
        {
            PodcastService.PodcastDownloadCompleted -= Podcast_Completed;

            ChaptersList = e.Value;
        }

        /// <summary>
        /// Executed when the petition of articles has been completed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void RssArticles_Completed(object sender, RssDownloadEventArgs e)
        {
            RssService.RssDownloadCompleted -= RssArticles_Completed;
            ArticlesList = e.Value;

            RssService.RssDownloadCompleted += new EventHandler<RssDownloadEventArgs>(RssTwitter_Completed);
            RssService.GetRssData(new Uri(@"http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=wpcontrola", UriKind.Absolute));
        }

        /// <summary>
        /// Executed when the petition of twitter has been completed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void RssTwitter_Completed(object sender, RssDownloadEventArgs e)
        {
            RssService.RssDownloadCompleted -= RssTwitter_Completed;
            TwitterList = e.Value;
        }

        Entities.Rss articlesList = new Entities.Rss();
        /// <summary>
        /// List of articles.
        /// </summary>
        public Entities.Rss ArticlesList
        {
            get
            {
                return articlesList;
            }
            set
            {
                articlesList = value;
                RaisePropertyChanged(() => ArticlesList);
            }
        }

        Entities.Podcast chaptersList = new Entities.Podcast();
        /// <summary>
        /// List of chapters.
        /// </summary>
        public Entities.Podcast ChaptersList
        {
            get
            {
                return chaptersList;
            }
            set
            {
                chaptersList = value;
                RaisePropertyChanged(() => ChaptersList);
            }
        }

        Entities.PodcastItem selectedChapter;
        /// <summary>
        /// Selected chapter
        /// </summary>
        public Entities.PodcastItem SelectedChapter 
        {
            get
            {
                return selectedChapter;
            }
            set
            {
                selectedChapter = value;
                NavService.NavigateToChapter(value);
            }
        }

        Entities.Rss twitterList = new Entities.Rss();
        /// <summary>
        /// List of tweets.
        /// </summary>
        public Entities.Rss TwitterList
        {
            get
            {
                return twitterList;
            }
            set
            {
                twitterList = value;
                RaisePropertyChanged(() => TwitterList);
            }
        }

        private ICommand previewAudioCommand;
        /// <summary>
        /// This command preview the podcast chapter.
        /// </summary>
        public ICommand PreviewAudioCommand
        {
            get
            {
                if (previewAudioCommand == null)
                    previewAudioCommand = new DelegateCommand<Entities.PodcastItem>(ExecutePreviewAudioCommand, null);
                return previewAudioCommand;
            }    
        }

        /// <summary>
        /// This method executes the command action.
        /// </summary>
        public void ExecutePreviewAudioCommand(Entities.PodcastItem podcast)
        {
            AudioService.PreviewTrack(podcast);
        }
    }
}
